package arcane.object.screen;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

import org.lwjgl.opengl.GL11;

import arcane.constant.Constants;
import arcane.object.game.engine.ArcaneEngine;
import arcane.object.game.sprite.GeometricSprite;
import arcane.object.game.sprite.MenuSprite;
import arcane.object.game.sprite.SigilSprite;
import arcane.object.gui.DefaultButton;
import arcane.object.gui.DefaultLabel;
import arcane.object.gui.DefaultPanel;

import com.golden.gamedev.GameEngine;
import com.golden.gamedev.GameObject;
import com.golden.gamedev.engine.BaseGraphics;
import com.golden.gamedev.engine.graphics.FullScreenMode;
import com.golden.gamedev.engine.graphics.WindowedMode;
import com.golden.gamedev.object.GameFont;
import com.golden.gamedev.util.ImageUtil;

public class TitleScreen extends GameObject implements ActionListener {
	GeometricSprite sigil;
	SigilSprite[] sigils;

	float outRotation = 0;
	float midRotation = 0;

	Color outSigilColor;
	Color sigilColor;

	float alphaOut;
	float alphaSigil;
	float alphaInc = .01f;
	float alphaGlowInc = .003f;

	boolean transition = true;
	boolean sigilGlow = false;
	boolean shrinking = true;
	boolean growing = false;

	BufferedImage title;
	DefaultPanel startPanel;
	GameFont font;

	MenuSprite[] startLbls;
	MenuSprite[] optionLbls;
	MenuSprite[] creditsLbls;

	public TitleScreen(GameEngine parent) {
		super(parent);
	}

	private BufferedImage getImg(String path) {
		try {
			return ImageUtil.getImage(ClassLoader.getSystemResource(path),
					Transparency.TRANSLUCENT);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public void initResources() {
		font = Constants.fontManager.getFont("MenuFont");// getImg("Images/Menu/font2.png"));
		startPanel = new DefaultPanel(Constants.getImage("Images/panel1.png"),
				240, 190);
		startPanel.setHeight(115);
		startPanel.setColor(Color.BLUE);
		startPanel.setAlpha(.3f);
		startPanel.setActive(false);

		initLabels();

		title = getImg("Images/Menu/title.png");
		outSigilColor = new Color(.7f, 0f, 0f);
		sigilColor = new Color(.6f, 0f, .6f);
		alphaOut = alphaSigil = 0;

		sigil = new GeometricSprite(getImg("Images/Menu/menuSigil.png"), 46, 0);
		sigil.setColor(sigilColor);
		sigil.setAlpha(0);

		sigils = new SigilSprite[3];
		String[] strImgs = new String[] { "sigilBottom.png", "sigilLeft.png",
				"sigilRight.png" };
		String[] cmds = new String[] { "OptionSigil", "CreditSigil",
				"StartSigil" };
		int[] locations = new int[] { 110, 180, 460, 66, 392, 394 };
		for (int i = 0; i < strImgs.length; i++) {
			sigils[i] = new SigilSprite(getImg("Images/Menu/" + strImgs[i]),
					locations[2 * i], locations[2 * i + 1]);
			sigils[i].setActionCommand(cmds[i]);
			sigils[i].addActionListener(this);
			sigils[i].setColor(outSigilColor);
			sigils[i].setAlpha(0);
		}
	}

	public void initLabels() {
		DefaultLabel newGame = createLabel("New Game");
		newGame.setEnabled(false);
		startLbls = new MenuSprite[] { newGame, createLabel("Load Game"),
				createLabel("Multiplayer"), createLabel("Quit") };
		optionLbls = new MenuSprite[] { createLabel(""),createLabel("Options"),
				createLabel("Map Editor") };
		creditsLbls = new MenuSprite[] {createLabel(""), createLabel("Credits"),
				createLabel("About") };
	}

	private DefaultButton createLabel(String text) {
		DefaultButton lbl = new DefaultButton(text, Color.RED);
		lbl.addActionListener(this);
		return lbl;
	}

	public void render(Graphics2D g) {
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, 640, 480);
		sigil.render(g);
		for (int i = 0; i < sigils.length; i++) {
			sigils[i].render(g);
		}
		g.drawImage(title, 140, 50, null);
		startPanel.render(g);
		GL11.glColor4f(1f, 0f, 0f, 1f);
	}

	public void update(long time) {
		if (transition && keyDown(KeyEvent.VK_ENTER)) {
			transition = false;
			alphaOut = alphaSigil = 1f;
		}
		startPanel.update(time);
		if (alphaOut < 1) {
			alphaOut += alphaInc;
			if (alphaOut > 1)
				alphaOut = 1;
		} else if (!sigilGlow) {
			alphaSigil += alphaInc;
			if (alphaSigil >= 1) {
				alphaSigil = 1;
				sigilGlow = true;
			}
		} else {
			transition = false;
			if (shrinking) {
				alphaSigil -= alphaGlowInc;
				if (alphaSigil <= .6f) {
					alphaSigil = .6f;
					shrinking = false;
					growing = true;
				}
			} else {
				alphaSigil += alphaGlowInc;
				if (alphaSigil >= 1f) {
					alphaSigil = 1f;
					shrinking = true;
					growing = false;
				}
			}
		}
		sigil.setAlpha(alphaSigil);

		outRotation += 1.5;
		midRotation -= 1.5;
		if (outRotation > 360)
			outRotation = 0;
		if (midRotation > 360)
			midRotation = 0;
		for (int i = 0; i < sigils.length; i++) {
			sigils[i].setAlpha(alphaOut);
			sigils[i].setAngle(outRotation);
			sigils[i].update(time);
		}
	}

	private void initStartPane(MenuSprite[] lbls) {
		if (transition) {
			alphaOut = alphaSigil = 1f;
		}
		transition = false;
		startPanel.removeAll();
		startPanel.setAlphaMaximum(.3f);
		startPanel.setAlpha(.3f);
		for (int i = 0; i < lbls.length; i++) {
			startPanel.add(lbls[i]);
		}
	}

	public void actionPerformed(ActionEvent e) {
		String s = e.getActionCommand();
		Object source = e.getSource();
		switch (e.getID()) {
		case MenuSprite.MOUSE_DOWN:
			if (s.equals("New Game")) {
				TitleScreen.this.parent.nextGameID = ArcaneEngine.PLAY_SCREEN;
				TitleScreen.this.finish();
			} else if (s.equals("Load Game")) {
				// TODO
			} else if (s.equals("Multiplayer")) {
				TitleScreen.this.parent.nextGameID=ArcaneEngine.MULTI_SCREEN;
				TitleScreen.this.finish();
			} else if (s.equals("Quit")) {
				TitleScreen.this.parent.nextGameID = -1;
				TitleScreen.this.finish();
			}
			break;
		case MenuSprite.MOUSE_ENTERED:
			if (s.equals("StartSigil")) {
				this.startPanel.setActive(true);
				initStartPane(startLbls);
			} else if (s.equals("OptionSigil")) {
				startPanel.setActive(true);
				initStartPane(optionLbls);
			} else if (s.equals("CreditSigil")) {
				startPanel.setActive(true);
				initStartPane(creditsLbls);
			}
		}
	}
}